package com.jumper.study.admin.service.impl;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jumper.study.admin.dto.req.StudentSearchReq;
import com.jumper.study.admin.service.StudentService;
import com.jumper.study.db.dao.StudentMapper;
import com.jumper.study.db.dto.StudentSearchDto;
import com.jumper.study.db.model.Student;

import cn.hutool.core.util.StrUtil;

@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    @Override
    public IPage<Student> listWithAccount(IPage<Student> page, StudentSearchReq req) {
        StudentSearchDto dto = new StudentSearchDto();
        if (!StrUtil.isEmpty(req.getTitle())) {
            switch(req.getField()) {
                case 1:
                    dto.setName(req.getTitle());
                    break;
                case 2:
                    dto.setUsername(req.getTitle());
                    break;
                case 3:
                    dto.setPhone(req.getTitle());
                    break;
                default:
                    dto.setName(req.getTitle());
                    break;
            }
        }
        return getBaseMapper().selectPageWithAccount(page, dto);
    }

    @Override
    public Student getByAccountId(Long accountId) {
        LambdaQueryWrapper<Student> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Student::getAccountId, accountId);

        return getOne(wrapper);
    }
    
}
